package me.numark.activities;

import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.textservice.TextInfo;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import me.numark.R;
import me.numark.model.Feed;
import me.numark.net.BaseResponse;
import me.numark.net.NetworkConstants;
import me.numark.net.RequestServerTask;
import me.numark.utils.Global;
import me.numark.utils.HttpUtil;
import me.numark.utils.Utils;
import me.numark.view.FeedViewHolder;

public class ReportActivity extends BaseActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    @Bind(R.id.right_text)
    TextView submit;
    @Bind(R.id.report_1)
    FrameLayout reportA;
    @Bind(R.id.report_2)
    FrameLayout reportB;
    @Bind(R.id.report_3)
    FrameLayout reportC;
    @Bind(R.id.report_4)
    FrameLayout reportD;
    @Bind(R.id.report_5)
    FrameLayout reportE;
    
    @Bind(R.id.checkbox_1)
    CheckBox checkBoxA;
    @Bind(R.id.checkbox_2)
    CheckBox checkBoxB;
    @Bind(R.id.checkbox_3)
    CheckBox checkBoxC;
    @Bind(R.id.checkbox_4)
    CheckBox checkBoxD;
    @Bind(R.id.checkbox_5)
    CheckBox checkBoxE;
    @Bind(R.id.toolbar)
    Toolbar toolbar;

    TextInputLayout textInputLayout;

    int reportType = -1;
    private long mId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_report;
        super.onCreate(savedInstanceState);
        mId = getIntent().getLongExtra(FeedViewHolder.FEED_ID, -1);
        if (mId==-1){
            Utils.showToast("请求失败");
            finish();
        }
        uiInit();
    }

    private void uiInit() {
        titleTextView.setText(R.string.more_report);
        submit.setVisibility(View.VISIBLE);
        submit.setText(R.string.submit_btn);
        submit.setOnClickListener(this);
        reportA.setOnClickListener(this);
        reportB.setOnClickListener(this);
        reportC.setOnClickListener(this);
        reportD.setOnClickListener(this);
        reportE.setOnClickListener(this);
        checkBoxA.setOnCheckedChangeListener(this);
        checkBoxB.setOnCheckedChangeListener(this);
        checkBoxC.setOnCheckedChangeListener(this);
        checkBoxD.setOnCheckedChangeListener(this);
        checkBoxE.setOnCheckedChangeListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.report_1:
                checkBoxA.setChecked(true);
                reportType = 1;
                break;
            case R.id.report_2:
                checkBoxB.setChecked(true);
                reportType = 2;
                break;
            case R.id.report_3:
                checkBoxC.setChecked(true);
                reportType = 3;
                break;
            case R.id.report_4:
                checkBoxD.setChecked(true);
                reportType = 4;
                break;
            case R.id.report_5:
                checkBoxE.setChecked(true);
                reportType = 5;
                break;
            case R.id.right_text:
                final String description = submit.getText().toString().trim();
                if (reportType == -1){
                    Utils.showToast(getString(R.string.please_select_report_type));
                    return;
                }
                new RequestServerTask<BaseResponse>(BaseResponse.class) {
                    @Override
                    protected String requestServer() {
                        Map<String,Object> map = new HashMap<String, Object>();
                        map.put("d_type", reportType);
                        map.put("description", description);
                        String url = String.format(NetworkConstants.FEED_REPORT_URL, Global.getCurrentUid(),mId);
                        return HttpUtil.post(url, map);
                    }

                    @Override
                    protected void onSuccess(BaseResponse result) {
                        Utils.showToast("提交成功");
                        finish();
                    }
                }.start();
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()){
            case R.id.checkbox_1:
                if (isChecked) {
                    checkBoxB.setChecked(false);checkBoxC.setChecked(false);
                    checkBoxD.setChecked(false);checkBoxE.setChecked(false);
                    reportType=1;
                }
                break;
            case R.id.checkbox_2:
                if (isChecked) {
                    checkBoxA.setChecked(false);checkBoxC.setChecked(false);
                    checkBoxD.setChecked(false);checkBoxE.setChecked(false);
                    reportType=2;
                }
                break;
            case R.id.checkbox_3:
                if (isChecked) {
                    checkBoxA.setChecked(false);checkBoxB.setChecked(false);
                    checkBoxD.setChecked(false);checkBoxE.setChecked(false);
                    reportType=3;
                }
                break;
            case R.id.checkbox_4:
                if (isChecked) {
                    checkBoxA.setChecked(false);checkBoxB.setChecked(false);
                    checkBoxC.setChecked(false);checkBoxE.setChecked(false);
                    reportType=4;
                }
                break;
            case R.id.checkbox_5:
                if (isChecked) {
                    checkBoxA.setChecked(false);checkBoxB.setChecked(false);
                    checkBoxC.setChecked(false);checkBoxD.setChecked(false);
                    reportType=5;
                }
                break;
        }
    }
}
